<?php

namespace apiend\controllers;

use Yii;
use apiend\components\Controller;
use apiend\models\LoginForm;
use apiend\models\SmsLoginForm;
use apiend\models\SigninForm;
use apiend\models\SignupForm;
use apiend\models\BindMobileForm;
use apiend\models\PasswordForgetRequestForm;
use apiend\models\PasswordResetRequestForm;

/**
 * 登录、注册、找回密码相关接口
 * PassportController
 */
class PassportController extends Controller {

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function verbs() {
        return [
            'mine' => ['GET'],
            '*' => ['POST'],
        ];
    }

    /**
     * Declares the allowed HTTP verbs.
     * Please refer to [[VerbFilter::actions]] on how to declare the allowed verbs.
     * @return array the allowed HTTP verbs.
     */
    protected function rules() {
        return [
            [
                'actions' => ['login', 'sms-login', 'signin', 'register', 'forget', 'bind-mobile'],
                'allow' => true,
            ],
            [
                'actions' => ['logout', 'mine', 'reset-password'],
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * 帐号登录
     * @return mixed
     */
    public function actionLogin() {
        $model = new LoginForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入帐号和密码信息');
        }
        $model->token = $this->token;
        return $model->login();
    }

    /**
     * 短信登录
     * @return mixed
     */
    public function actionSmsLogin() {
        $model = new SmsLoginForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入手机号和短信验证码');
        }
        $model->token = $this->token;
        return $model->login();
    }

    /**
     * 短信登录/注册
     * @return mixed
     */
    public function actionSignin() {
        $model = new SigninForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入手机号和短信验证码');
        }
        return $model->signin();
    }

    /**
     * 新用户注册
     * @return mixed
     */
    public function actionRegister() {
        $model = new SignupForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入用户注册所需要的信息');
        }
        return $model->signup();
    }

    /**
     * 找回密码
     * @return mixed
     */
    public function actionForget() {
        $model = new PasswordForgetRequestForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入要重置的帐号手机号和短信验证码');
        }
        return $model->send();
    }

    /**
     * 找回密码
     * @return mixed
     */
    public function actionResetPassword() {
        $model = new PasswordResetRequestForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入要重置的帐号手机号和短信验证码');
        }
        $model->user_id = $this->token->user_id;
        return $model->send();
    }

    /**
     * 退出登录
     */
    public function actionLogout() {
        $token = $this->token;
        if ($token->user_id == 0) {
            return parent::error('该用户已登出，退出登录失败');
        }
        $token->user_id = 0;
        return $token->save();
    }

    /**
     * 重新登录
     */
    public function actionMine() {
        return $this->user ? $this->user->meta : [];
    }

    /**
     * 绑定手机号
     */
    public function actionBindMobile() {
        $model = new BindMobileForm();
        if (!$model->load(Yii::$app->request->post(), '')) {
            parent::error('请输入手机号');
        }
        $model->token = $this->token;
        return $model->bind();
    }

    public function afterAction($action, $result) {
        if (in_array($action->id, ['login', 'signin', 'sms-login', 'register'])) {
            if (!$this->token->bindUser($result->id)) {
                parent::error('登录失败', $this->token->firstErrors);
            }
        }
        return parent::afterAction($action, $result);
    }

}
